package com.gx.recruitment.controller;

import com.gx.recruitment.entity.*;
import com.gx.recruitment.service.CompJobService;
import com.gx.recruitment.service.CompReResumeService;
import com.gx.recruitment.service.UserResumeService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class CompReResumeController {
    @Resource
    private CompReResumeService compReResumeService;
    @Resource
    private UserResumeService userResumeService;
    @Resource
    private CompJobService compJobService;

    @PostMapping("/compReResume/add")
    public Map<String, Object> insertReResume(Integer userid, Integer jobid, Integer compid) {
        Map<String, Object> map = new HashMap<>();

        User_resumeExample example1 = new User_resumeExample();
        User_resumeExample.Criteria cri = example1.createCriteria();
        cri.andUserIdEqualTo(userid);
        List<User_resume> resumes = userResumeService.selectByResumeId(example1);
        Comp_receiveresume comp_receiveresume = new Comp_receiveresume();
        comp_receiveresume.setResumeName(resumes.get(0).getResumeName());
        comp_receiveresume.setResumeSex(resumes.get(0).getResumeSex());
        comp_receiveresume.setResumeBirth(resumes.get(0).getResumeBirth());
        comp_receiveresume.setResumeAddress(resumes.get(0).getResumeAddress());
        comp_receiveresume.setResumeEdu(resumes.get(0).getResumeEdu());
        comp_receiveresume.setResumeExpr(resumes.get(0).getResumeExpr());
        comp_receiveresume.setResumePhone(resumes.get(0).getResumePhone());
        comp_receiveresume.setResumeEmail(resumes.get(0).getResumeEmail());
        comp_receiveresume.setJobName(resumes.get(0).getJobName());
        comp_receiveresume.setJobIndustry(resumes.get(0).getJobIndustry());
        comp_receiveresume.setJobAddress(resumes.get(0).getJobAddress());
        comp_receiveresume.setJobType(resumes.get(0).getJobType());
        comp_receiveresume.setJobPay(resumes.get(0).getJobPay());
        comp_receiveresume.setUserId(userid);
        comp_receiveresume.setJobId(jobid);
        comp_receiveresume.setCompId(compid);

        Comp_receiveresumeExample example = new Comp_receiveresumeExample();
        Comp_receiveresumeExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userid);
        criteria.andJobIdEqualTo(jobid);
        List<Comp_receiveresume> list = compReResumeService.selectByExample(example);

        if (list.size() > 0) {
            map.put("msg", "已经申请过了");
        } else {
            int res = compReResumeService.insertReResume(comp_receiveresume);
            if (res == 0) {
                map.put("status", 200);
                map.put("msg", "申请成功");
            } else {
                map.put("msg", "申请失败");
            }
        }
        return map;
    }

    @PostMapping("/compReResume/getResume")
    public List<Comp_receiveresume> getResume(Integer compid){
        Comp_receiveresumeExample example = new Comp_receiveresumeExample();
        Comp_receiveresumeExample.Criteria criteria = example.createCriteria();
        criteria.andCompIdEqualTo(compid);
        List<Comp_receiveresume> resumes = compReResumeService.selectByExample(example);
        return resumes;
    }

    @PostMapping("/compReResume/deleteone")
    public Map<String,Object> deleteResume(Integer resumeid){
        Map<String, Object> map = new HashMap<>();
        int res = compReResumeService.deleteByPrimaryKey(resumeid);
        if (res == 0) {
            map.put("status", 200);
            map.put("msg", "删除成功");
        } else {
            map.put("msg", "删除失败");
        }
        return map;
    }
}